package dimensions

import (
	"encoding/json"
	"strconv"
	"oxy_server/model"
	"oxy_server/module/log"
	"oxy_server/until"

	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
)

type (
	Dimensions struct {
		ID               bson.ObjectId `json:"_id" bson:"_id"`
		Kind             string        `json:"kind" bson:"kind"`                         // 品类
		Version          string        `json:"version" bson:"version"`                   // 版型
		Position         string        `json:"position" bson:"position"`                 // 部位
		Standard         string        `json:"standard" bson:"standard"`                 // 度法
		SampleL          float64       `json:"sampleL" bson:"sampleL"`                   //打样尺码L
		Sampledeviation  float64       `json:"sampledeviation" bson:"sampledeviation"`   //  打样误差
		ProductL         float64       `json:"productL" bson:"productL"`                 // 产前尺码L
		Productdeviation float64       `json:"productdeviation" bson:"productdeviation"` // 产前误差
		Leveldeviation   float64       `json:"leveldeviation" bson:"leveldeviation"`     // 档差
		Publicdeviation  float64       `json:"publicdeviation" bson:"publicdeviation"`   // 公差
		XS               float64       `json:"XS" bson:"XS"`
		S                float64       `json:"S" bson:"S"`
		M                float64       `json:"M" bson:"M"`
		L                float64       `json:"L" bson:"L"`
		XL               float64       `json:"XL" bson:"XL"`
		XXL              float64       `json:"XXL" bson:"XXL"`
		XXXL             float64       `json:"XXXL" bson:"XXXL"`
		Note             string        `json:"note" bson:"note"`     // 备注
		Status           bool          `json:"status" bson:"status"` //是否可以同步
	}
	InsertDimensions struct {
		Kind             string  `json:"kind" bson:"kind"`                         // 品类
		Version          string  `json:"version" bson:"version"`                   // 版型
		Position         string  `json:"position" bson:"position"`                 // 部位
		Standard         string  `json:"standard" bson:"standard"`                 // 度法
		SampleL          float64 `json:"sampleL" bson:"sampleL"`                   //打样尺码L
		Sampledeviation  float64 `json:"sampledeviation" bson:"sampledeviation"`   //  打样误差
		ProductL         float64 `json:"productL" bson:"productL"`                 // 产前尺码L
		Productdeviation float64 `json:"productdeviation" bson:"productdeviation"` // 产前误差
		Leveldeviation   float64 `json:"leveldeviation" bson:"leveldeviation"`     // 档差
		Publicdeviation  float64 `json:"publicdeviation" bson:"publicdeviation"`   // 公差
		XS               float64 `json:"XS" bson:"XS"`
		S                float64 `json:"S" bson:"S"`
		M                float64 `json:"M" bson:"M"`
		L                float64 `json:"L" bson:"L"`
		XL               float64 `json:"XL" bson:"XL"`
		XXL              float64 `json:"XXL" bson:"XXL"`
		XXXL             float64 `json:"XXXL" bson:"XXXL"`
		Note             string  `json:"note" bson:"note"`     // 备注
		Status           bool    `json:"status" bson:"status"` //是否可以同步
	}
)
type OXYDimensions struct{}

func NewOXYDimensions() *OXYDimensions {
	Data := &OXYDimensions{}
	return Data
}

func (ro *OXYDimensions) GetDimensionsListHandler(c echo.Context) error {
	currentpage, err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage, err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	sort := c.QueryParam("sort")
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "dimensions"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	query, count, err := model.DB().QuerySort(M, currentpage, perpage, sort)
	if err != nil {
		log.Error("查询出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	ps := []Dimensions{}
	query.All(&ps)

	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":  string(str),
		"count": count,
	})

	return nil

}

func (ro *OXYDimensions) GetDimensionsInfoHandler(c echo.Context) error {
	log.Info("GetRoleInfoHandler")
	id := c.QueryParam("id")
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "dimensions"
	query, err := model.DB().QueryOne(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return nil
	}
	p := Dimensions{}
	query.One(&p)
	str, err := json.Marshal(p)
	if err != nil {
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return nil
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
		"data": string(str),
	})

	return nil
}

func (ro *OXYDimensions) AddDimensionHandler(c echo.Context) error {
	s := &InsertDimensions{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyDic"
	model.DB().Document = "dimensions"
	err := model.DB().Insert(s)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}

func (ro *OXYDimensions) AddDimensionsHandler(c echo.Context) error {
	s := []InsertDimensions{}
	if err := c.Bind(&s); err != nil {
		log.Error("AddPropertysHandler 解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyDic"
	model.DB().Document = "dimensions"
	var count = 0
	for _, item := range s {
		err := model.DB().Insert(item)
		if err != nil {
			count++
			log.Error("插入出错 AddPropertysHandler", err)
		}
	}
	if count > 0 {
		c.JSON(200, map[string]interface{}{
			"msg":  "warning",
			"info": count,
		})
	} else {
		c.JSON(200, map[string]interface{}{
			"msg": "success",
		})
	}

	return nil
}

func (ro *OXYDimensions) UpdateDimensionsHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("UpdateSupplierTableHandler", id)

	s := &InsertDimensions{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyDic"
	model.DB().Document = "dimensions"
	err := model.DB().Update(bson.M{"_id": bson.ObjectIdHex(id)}, s)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
func (ro *OXYDimensions) DelDimensionsHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelSupplierTableHandler", id)
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "dimensions"
	err := model.DB().Remove(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		log.Error("删除出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
